using UnityEngine;
using QFramework;

namespace QFramework.FlappyBird
{
	public partial class Pipe : ViewController
	{
		private float speed = 0.75f;
		private bool alreadyAdd = false;

		void Start()
		{
			// Code Here
			FlappyBird.GameState.Register(gameState =>
			{
				if(gameState == GameStates.GameOver)
				{
					gameObject.DestroySelf();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

        private void Update()
        {
			transform.LocalPositionX(transform.localPosition.x - speed * Time.fixedDeltaTime);

			if(transform.localPosition.x < -2.55 && !alreadyAdd && FlappyBird.GameState.Value == GameStates.Started)
			{
				FlappyBird.Score.Value++;
				alreadyAdd = true;
				AudioKit.PlaySound("COIN");
			}
			if(transform.localPosition.x < -20)
			{
				PipeGenarator.PipePool.Recycle(this);
			}
        }

		public void ResetData()
		{
			this.Hide();
			alreadyAdd = false;
		}
    }
}
